﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Gameplay.RTS.ScriptConfig;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine;
using Yoozoo.Framework.Managers;
using Yoozoo.Managers;
using Yoozoo.Mars.Got;
using Yoozoo.Gameplay.RTS;

public class RTSFlyTextHelper : EditorWindow
{
    private Dictionary<int, string> flyTxtButton = new Dictionary<int, string>
    {
        {8,"其他人造成的普攻飘字"},
        {81,"被我攻击的普攻飘字"},
        {82,"我被攻击的普攻飘字"},
        {83,"被动技能"},
        {91,"我释放的怒气技能飘字"},
        {92,"我收到的技能颜色"},
        {10,"治疗"},
        {11,"AttackCity"},
        {12,"SkillDamageName"},
        {13,"SkillBuffName"},
        {14,"SkillHealName"},
        {15,"NormalHeal"},
        {1000,"夹击"},
        {1001,"击溃"},
        {1002,"连击"},
        {1003,"战败"},
        {1005,"开战"},
        {1006,"1006"},
        {2000,"我方技能"},
        {2001,"敌方技能"},
    };
    private Vector2 pos = new Vector2(0, 0);
    private AnimBool fadeGroup;
    private FlyTextConfig config;
    private void OnEnable()
    {
        fadeGroup = new AnimBool(false);
        // 注册动画监听
        fadeGroup.valueChanged.AddListener(Repaint);
    }
    private void OnDisable()
    {
        // 移除动画监听
        fadeGroup.valueChanged.RemoveListener(Repaint);
    }

    [MenuItem("GTATools/GamePlay/RTS/RTS飘字工具", false, 0)]
    private static void OpenParticleHelperWindow()
    {
        RTSFlyTextHelper win = GetWindow<RTSFlyTextHelper>("RTS飘字工具");
        win.Show();
    }

    private void OnGUI()
    {
#if UNITY_EDITOR
        fadeGroup.target = EditorGUILayout.Foldout(fadeGroup.target,"飘字工具",true);
        pos = EditorGUILayout.BeginScrollView(pos);
        if (EditorGUILayout.BeginFadeGroup(fadeGroup.faded))
        {
            if (ArmyManager.Instance != null)
            {
                config = ArmyManager.Instance.ResourceManager.JsonManager.GetFlyTextConfig();
                if (config!=null)
                {
                    config.ScaleSpecial = EditorGUILayout.FloatField("特殊类飘字缩放大小", config.ScaleSpecial);
                    config.ScaleNormal = EditorGUILayout.FloatField("飘字缩放大小", config.ScaleNormal);
                    config.columSpace = EditorGUILayout.FloatField("列距", config.columSpace);
                    config.AnimSpeed = EditorGUILayout.FloatField("飘字动画速度", config.AnimSpeed);
                    EditorGUILayout.Space();
                }
            }
           
            var enumerator = flyTxtButton.GetEnumerator();

            while (enumerator.MoveNext())
            {
                EditorGUILayout.BeginVertical();
                if (ArmyManager.Instance != null)
                {
                    var txtConfig = ArmyManager.Instance.ResourceManager.JsonManager.GetFlyTextOffsetByType( enumerator.Current.Key);
                    var type = txtConfig.GetType();
                    var GpuTextAsset = AssetDatabase.LoadAssetAtPath<GpuHudAsset>(
                        $"Assets/ResourcesAssets/Config/GPUHUDConfig/FlyText/FlyText{enumerator.Current.Key}.asset");
                   
                    foreach (var propertyInfo in type.GetProperties())
                    {
                        var name = propertyInfo.Name;
                        var value = propertyInfo.GetValue(txtConfig);
                        var protertyType = value?.GetType() ?? typeof(object);

                        if (protertyType == typeof(Single))
                        {
                            var result = EditorGUILayout.FloatField(name, (float) value);
                            propertyInfo.SetValue(txtConfig,result);
                        }
                        else if (protertyType == typeof(Int32))
                        {
                            if (name == "FontSize" && GpuTextAsset != null)
                            {
                                foreach (var textList in GpuTextAsset.m_TextAssetList)
                                {
                                    var showSize = textList.OriginSize * 32.5f;
                                    var size  = EditorGUILayout.FloatField(name, showSize);
                                    textList.OriginSize = size / 32.5f;
                                }
                            }
                            else if (name == "AnimationStyle" && GpuTextAsset != null)
                            {
                                foreach (var textList in GpuTextAsset.m_TextAssetList)
                                {
                                    textList.flyAnimType  = (GpuFlyTextAnim)EditorGUILayout.EnumPopup(textList.flyAnimType,"AnimationStyle");
                                }
                            }
                            else
                            {
                                var result = EditorGUILayout.IntField(name, (int) value);
                                propertyInfo.SetValue(txtConfig,result);
                            }
                            
                        }
                        else if (protertyType == typeof(bool))
                        {
                            var result = EditorGUILayout.Toggle(name, (bool) value);
                            propertyInfo.SetValue(txtConfig,result);
                        }
                        else if (protertyType == typeof(string) && name == "VertextColor")
                        {
                            if (GpuTextAsset!=null)
                            {
                                foreach (var textList in GpuTextAsset.m_TextAssetList)
                                {
                                    var originData = textList.ShaderOriginData;
                                    var color = EditorGUILayout.ColorField(name, originData.FaceColor);
                                    textList.SetColor(color);
                                }
                            }
                            else
                            {
                                if (ColorUtility.TryParseHtmlString((string)value, out Color color))
                                {
                                    var result = EditorGUILayout.ColorField(name, color);
                                    propertyInfo.SetValue(txtConfig, "#"+ColorUtility.ToHtmlStringRGB(result));
                                }
                            }
                        }else if (protertyType == typeof(string))
                        {
                            var result = EditorGUILayout.TextField(name,  (string)value);
                            propertyInfo.SetValue(txtConfig,result);
                        }
                    }
                }
            
                if (GUILayout.Button(enumerator.Current.Value))
                {
//                    if (enumerator.Current.Key == 8 || enumerator.Current.Key == 9)
//                    {
//                        LuaManager.mainState.GetFunction("G_MarchManager.TestFlyText").Call(enumerator.Current.Key, 12345);
//                    }
                    if (enumerator.Current.Key == 2000 || enumerator.Current.Key == 2001)
                    {
                        LuaManager.mainState.GetFunction("G_MarchManager.TestFlyTextSkill").Call(enumerator.Current.Key,enumerator.Current.Value);
                    }
                    else
                    {
                        LuaManager.mainState.GetFunction("G_MarchManager.TestFlyText").Call(enumerator.Current.Key, enumerator.Current.Value);
                    }
                }

               
                EditorGUILayout.EndVertical();
                EditorGUILayout.Space();
            }
            enumerator.Dispose();
        }

        if (GUILayout.Button("连击"))
        {
            LuaManager.mainState.GetFunction("G_MarchManager.TestFlyRepeat").Call(FlyTextManager.Skill,"10");
        }

        EditorGUILayout.EndFadeGroup();
        if (GUILayout.Button("保存飘字配置"))
        {
            if (ArmyManager.Instance != null && ArmyManager.Instance.ResourceManager == null)
            {
                EditorUtility.DisplayDialog("提示", "请进入沙盘之后再点此按钮","确认");
            }
            else
            {
                ArmyManager.Instance.ResourceManager.JsonManager.SaveFlyTextOffsetConfig();
                AssetDatabase.SaveAssets();
            }
        }
        
        int id = EditorPrefs.GetInt("skillId");
        id = EditorGUILayout.IntField("技能ID",id);
        EditorPrefs.SetInt("skillId",id);

        if (GUILayout.Button("播放技能"))
        {
            
            LuaManager.mainState.GetFunction("marchTest.TestSkillById").Call(id,2);
        }
        
        EditorGUILayout.EndScrollView();
#endif   
    }
    
}
